package com.telecom.restaurant.controllor;

import com.telecom.restaurant.constant.DataResultConstant;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.service.AdminService;
import com.telecom.restaurant.service.OrderService;
import com.telecom.restaurant.service.WxService;
import com.telecom.restaurant.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by qiqi on 2019/12/25.
 */
@Controller
public class IndexController {

    @Autowired
    private WxService wxTokenService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/alive")
    @ResponseBody
    public DataResult alive() {

        return DataResultConstant.SUCCESS;
    }


    @RequestMapping("/getToken")
    @ResponseBody
    public DataResult getToken() {

        String token = wxTokenService.getAccessTokenFromCache();

        DataResult dataResult = DataResultConstant.buildSuccessResult(token);
        return dataResult;
    }

    @RequestMapping("/testUserToken")
    @ResponseBody
    public DataResult testUserToken(Long userId, String mobile) {

        String token = adminService.generateUseToken(userId, mobile);
        DataResult dataResult = DataResultConstant.buildSuccessResult(token);
        return dataResult;
    }

    @RequestMapping("/testPayCallBack")
    @ResponseBody
    public DataResult testPayCallBack(Long orderId) {

        if (orderId == null) {
            return DataResultConstant.WRONG_PARAM;
        }
        boolean result = orderService.payOrder(orderId);
        DataResult dataResult = DataResultConstant.buildSuccessResult(result);
        return dataResult;
    }

    @RequestMapping("/testEncrypt")
    @ResponseBody
    public DataResult testEncrypt(String data) {

        String token = EncryptUtil.encrypt(data);

        DataResult dataResult = DataResultConstant.buildSuccessResult(token);
        return dataResult;
    }
}
